package com.bawei.shoppingcar.json_demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.bawei.shoppingcar.R;
import com.bawei.shoppingcar.entity.GoodsEntity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class GsonActivity extends AppCompatActivity {
    private String path = "http://43.143.146.165:8083/goods/getAllGoods?goods_category=0";
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 101){
                String json = (String) msg.obj;
                //Gson解析
//                GoodsEntity goodsEntity = new Gson().fromJson(json, GoodsEntity.class);
//                List<GoodsEntity.DataBean> data = goodsEntity.getData();
//                for (GoodsEntity.DataBean datum : data) {
//                    Log.d("TAG", "data: "+datum);
//                }
                //FastJson解析
                GoodsEntity goodsEntity = JSON.parseObject(json, GoodsEntity.class);
                List<GoodsEntity.DataBean> data = goodsEntity.getData();
                for (GoodsEntity.DataBean datum : data) {
                    Log.d("TAG", "data: "+datum);
                }

            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jsonactivity);
        new GoodsThread().start();
    }
    class GoodsThread extends Thread{
        @Override
        public void run() {
            super.run();
            try {
                URL url=new URL(path);
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                httpURLConnection.setReadTimeout(10*1000);
                httpURLConnection.setConnectTimeout(10*1000);
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.connect();
                if(httpURLConnection.getResponseCode()==200)
                {
                    InputStream inputStream=httpURLConnection.getInputStream();
                    byte[] buffer=new byte[1024];
                    int len=0;
                    StringBuilder stringBuilder=new StringBuilder();
                    while ((len=inputStream.read(buffer))!=-1)
                    {
                        stringBuilder.append(new String(buffer,0,len));
                    }
                    //使用handler发送数据
                    Message message = Message.obtain();
                    message.what = 101;
                    message.obj = stringBuilder.toString();
                    mHandler.sendMessage(message);
                }
            } catch (MalformedURLException e) {   e.printStackTrace();
            } catch (IOException e) {  e.printStackTrace();
            }
        }
    }
}